package com.syni.mdd.yhd.common.all.controller.protect;

/**
 * @className BmsBusinessController
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/15 14:25
 * @VERSION 1.0
 **/

import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckParams;
import com.syni.mdd.sjb.common.component.utils.common.annotations.EntranceCheck;
import com.syni.mdd.sjb.common.component.utils.common.utils.PatternMatchUtils;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.dto.requestDto.order.BusinessOrderDetailReDto;
import com.syni.mdd.yhd.common.all.service.BmsBusinessService;
import com.syni.mdd.yhd.common.all.service.BmsUserBusinessService;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.yhd.common.all.utils.StringUtils;
import com.syni.mdd.yhd.common.all.validationGroup.GroupCheckPaging;
import com.syni.mdd.yhd.common.all.validationGroup.GroupFind;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/userBusiness")
public class BmsUserBusinessController {
    @Autowired
    BmsUserBusinessService bmsUserBusinessService;

    @Autowired
    BmsBusinessService bmsBusinessService;


    @PostMapping("/registerBusiness")
    @CheckParams(params = {"phone","identify_code"})
    public Map<String,Object> registerBussiness(@RequestBody ParamsMap paramsMap){
        return bmsUserBusinessService.userRegisterBussiness(paramsMap);
    }

    @PostMapping("/loginBusiness")
    @CheckParams(params = {"phone","login_way","login_body"})
    public Map<String,Object> loginBussiness(@RequestBody Map<String,Object> paramsMap){
        String phone = paramsMap.get("phone").toString();
        Integer loginWay = Integer.parseInt(paramsMap.get("login_way").toString());
        String loginBody = paramsMap.get("login_body").toString();
        return bmsUserBusinessService.userLoginBussiness(phone,loginWay,loginBody);
    }

    @PostMapping("/getLoginBusinessData")
    @CheckParams(checkAuth = true)
    public Map<String,Object> getLoginBusinessData(@RequestBody ParamsMap paramsMap){
        return bmsUserBusinessService.getLoginBusinessData(paramsMap);
    }


    @PostMapping("/claimBusiness")
    @CheckParams(checkAuth = true,params = {"business_id"})
    public Map<String,Object> claimBusiness(@RequestBody ParamsMap paramsMap){
        Integer userId = paramsMap.getInteger("userIdentify");
        Integer businessId = paramsMap.getInteger("business_id");
        String idCardPathPo = paramsMap.getString("id_card_path_po");
        String idCardPathBa = paramsMap.getString("id_card_path_ba");
        String invitationCode = paramsMap.getString("invitationCode");
        return bmsUserBusinessService.doClaimBusiness(userId,businessId,idCardPathPo,idCardPathBa,invitationCode);
    }

    @PostMapping("certificationBusiness")
    @CheckParams(checkAuth = true,params = {"business_id"})
    public Map<String,Object> certificationBusiness(@RequestBody ParamsMap paramsMap){
        return bmsUserBusinessService.doCertificationBussiness(paramsMap);
    }


    @PostMapping("/createBusiness")
    @CheckParams(checkAuth = true,params = {"business_name","addr","addr_detail","latitude",
            "longitude","phone"})
    public synchronized Map<String,Object> createBusiness(@RequestBody ParamsMap paramsMap){
        return bmsBusinessService.createBusiness(paramsMap);
    }

    @PostMapping("/updateBusiness")
    @CheckParams(checkAuth = true,params = {"business_id"})
    public Map<String,Object> updateBusiness(@RequestBody ParamsMap paramsMap){
        return bmsBusinessService.updateBusiness(paramsMap);
    }

    @PostMapping("/assistantList")
    @CheckParams(checkAuth = true,params = {"business_id","page_num","page_num","page_size"})
    public Map<String,Object> assistantList(@RequestBody Map<String,Object> paramsMap){
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Integer pageNum = Integer.parseInt(paramsMap.get("page_num").toString());
        Integer pageSize = Integer.parseInt(paramsMap.get("page_size").toString());
        return bmsUserBusinessService.assistantList(userId,businessId,pageNum,pageSize);
    }

    @PostMapping("/addBusinessMember")
    @CheckParams(checkAuth = true,params = {"business_id","assistant_name","phone","identify_code"})
    public Map<String,Object> addBusinessMember(@RequestBody Map<String,Object> paramsMap){
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        String assistantName = paramsMap.get("assistant_name").toString();
        final int ASSISTANT_NAME_MAX = 20;
        if(assistantName.length() >= ASSISTANT_NAME_MAX){
            return ResultMapHelper.failure("店员姓名过长");
        }
        String remark = "无备注";
        if(paramsMap.containsKey("remark") && StringUtils.checkNullAndEmpty(paramsMap.get("remark"))){
            remark = paramsMap.get("remark").toString();
        }
        if(remark.length() > ASSISTANT_NAME_MAX){
            return ResultMapHelper.failure("备注过长");
        }
        String phone = paramsMap.get("phone").toString();
        if(!PatternMatchUtils.checkPhone(phone)){
            return ResultMapHelper.result(ResultCode.CODE_USER_PHONE_ERROR,"手机号格式错误");
        }
        String identifyCode = paramsMap.get("identify_code").toString();
        return bmsUserBusinessService.addBusinessAssistant(userId,businessId,assistantName,remark,phone,identifyCode);
    }

    @PostMapping("/authAssistantRole")
    @CheckParams(checkAuth = true,params = {"business_id","assistant_id","auth_code"})
    public Map<String,Object> authAssistantRole(@RequestBody Map<String,Object> paramsMap){
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Integer assistantId = Integer.parseInt(paramsMap.get("assistant_id").toString());
        Integer authCode = Integer.parseInt(paramsMap.get("auth_code").toString());
        return bmsUserBusinessService.authAssistantRole(userId,businessId,assistantId,authCode);
    }

    @PostMapping("/deleteAssistant")
    @CheckParams(checkAuth = true,params = {"business_id","assistant_id"})
    public Map<String,Object> deleteAssistant(@RequestBody Map<String,Object> paramsMap){
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Integer assistantId = Integer.parseInt(paramsMap.get("assistant_id").toString());
        return bmsUserBusinessService.deleteAssistant(userId,businessId,assistantId);
    }

    @PostMapping("/assistantTransfer")
    @CheckParams(checkAuth = true,params = {"business_id","assistant_id"})
    public Map<String,Object> assistantTransfer(@RequestBody Map<String,Object> paramsMap){
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Integer assistantId = Integer.parseInt(paramsMap.get("assistant_id").toString());
        return bmsUserBusinessService.transferAssisant(userId,businessId,assistantId);
    }

    @PostMapping("/topVideo")
    @CheckParams(checkAuth = true,params = {"business_id","video_id"})
    public Map<String,Object> topVideo(@RequestBody Map<String,Object> paramsMap){
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Integer videoId = Integer.parseInt(paramsMap.get("video_id").toString());
        return bmsUserBusinessService.doTopVideo(userId,businessId,videoId);
    }

    @PostMapping("/delVideo")
    @CheckParams(checkAuth = true,params = {"business_id","video_id"})
    public Map<String,Object> delVideo(@RequestBody Map<String,Object> paramsMap){
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Integer videoId = Integer.parseInt(paramsMap.get("video_id").toString());
        return bmsUserBusinessService.delVideo(userId,businessId,videoId);
    }

    @PostMapping("/getMyBusinessHome")
    @CheckParams(checkAuth = true,params = {"business_id"})
    public Map<String,Object> getMyBusinessHome(@RequestBody Map<String,Object> paramsMap){
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        String latitude = null;
        String longitude = null;
        if(paramsMap.containsKey("latitude") && !"".equals(latitude)){
            latitude = paramsMap.get("latitude").toString();
        }
        if(paramsMap.containsKey("longitude") && !"".equals(longitude)){
            longitude = paramsMap.get("longitude").toString();
        }
        Integer pageSize = 9;
        if(paramsMap.containsKey("page_size") && !"".equals(paramsMap.get("page_size").toString())){
            pageSize = Integer.parseInt(paramsMap.get("page_size").toString());
        }
        return bmsUserBusinessService.getMyBusinessHome(userId,businessId,latitude,longitude,pageSize);
    }

    @PostMapping("/getMyBusinessVideo")
    @CheckParams(checkAuth = true,params = {"business_id","page_num","page_size"})
    public Map<String,Object> getBusinessVideo(@RequestBody Map<String,Object> paramsMap){
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        String latitude = null;
        String longitude = null;
        if(paramsMap.containsKey("latitude")){
            latitude = paramsMap.get("latitude").toString();
        }
        if(paramsMap.containsKey("longitude")){
            longitude = paramsMap.get("longitude").toString();
        }
        Integer pageNum = Integer.parseInt(paramsMap.get("page_num").toString());
        Integer pageSize = Integer.parseInt(paramsMap.get("page_size").toString());
        return bmsUserBusinessService.findMyBusinessVideo(userId,businessId,pageNum,pageSize,latitude,longitude);
    }

    @PostMapping("/getMyBusinessFocus")
    @CheckParams(checkAuth = true,params = {"business_id","page_num","page_size"})
    public Map<String,Object> getMyBusinessFocus(@RequestBody Map<String,Object> paramsMap){
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Integer pageNum = Integer.parseInt(paramsMap.get("page_num").toString());
        Integer pageSize = Integer.parseInt(paramsMap.get("page_size").toString());
        return bmsUserBusinessService.findMyBusinessFouce(userId,businessId,pageNum,pageSize);
    }

    @PostMapping("/releaseVideo")
    @CheckParams(checkAuth = true,params = {"business_id","video_file_id","video_file_url"})
    public Map<String,Object> releaseVideo(@RequestBody Map<String,Object> paramsMap){
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        String videoFileId = paramsMap.get("video_file_id").toString();
        String videoFileUrl = paramsMap.get("video_file_url").toString();;
        String videoSnapshot = null;
        if(paramsMap.containsKey("video_snapshot") && !"".equals(paramsMap.get("video_snapshot").toString())){
            videoSnapshot = paramsMap.get("video_snapshot").toString();
        }
        //视频描述
        String videoDescribe = null;
        if(paramsMap.containsKey("videoDescribe") && StringUtils.checkNullAndEmpty(paramsMap.get("videoDescribe"))){
            videoDescribe = paramsMap.get("videoDescribe").toString();
        }
        Integer musicId = null;
        if(paramsMap.containsKey("musicId") && StringUtils.checkNullAndEmpty(paramsMap.get("musicId"))){
            musicId = Integer.parseInt(paramsMap.get("musicId").toString());
        }
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        return bmsUserBusinessService.releaseVideo(userId,businessId,
                videoFileId,videoFileUrl,videoSnapshot,videoDescribe,musicId);
    }


    @PostMapping("/checkIsChangeVendorName")
    @CheckParams(checkAuth = true,params = {"businessId"})
    public Map<String,Object> checkIsChangeVendorName(@RequestBody Map<String,Object> paramsMap){
        Integer businessId  = Integer.parseInt(paramsMap.get("businessId").toString());
        return bmsBusinessService.checkIsChangeVendorName(businessId);
    }

    /**
     * @Description 获取商家订单首页数据
     * @Author: cfx
     * @Date: 2019/7/29
     */
    @PostMapping("/getMyOrderHome")
    @CheckParams(checkAuth = true,params = {"businessId"})
    public Map<String,Object> getMyOrderHome(@RequestBody Map<String,Object> paramsMap){
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        Integer businessId  = Integer.parseInt(paramsMap.get("businessId").toString());
        return bmsUserBusinessService.getMyOrderHome(userId,businessId);
    }

    /**
     * @Description 获取商家类型订单数据
     * @Author: cfx
     * @Date: 2019/7/29
     */
    @PostMapping("/getMyOrderByType")
    @EntranceCheck(checkAuth = true)
    public Map<String,Object> getMyOrderByType(@RequestBody @Validated({GroupFind.class, GroupCheckPaging.class}) BusinessOrderDetailReDto businessOrderDetailRePto){
        return bmsUserBusinessService.getMyOrderByType(businessOrderDetailRePto);
    }

    /**
     * @Description 获取商家的消费用户
     * @Author: cfx
     * @Date: 2019/7/29
     */
    @PostMapping("/getMyConsumerUsers")
    @CheckParams(checkAuth = true,params = {"businessId","pageNum","pageSize"})
    public Map<String,Object> getMyConsumerUsers(@RequestBody Map<String,Object> paramsMap){
        return bmsUserBusinessService.getMyConsumerUsers(paramsMap);
    }

    /**
     * @Description 输入码核销
     * @Author: cfx
     * @Date: 2019/7/29
     */
    @PostMapping("/searchCode")
    @CheckParams(checkAuth = true,params = {"businessId","code"})
    public Map<String,Object> searchCode(@RequestBody ParamsMap paramsMap){
        return bmsUserBusinessService.searchCode(paramsMap);
    }

    /**
     * @Description 金日收益
     * @Author: cfx
     * @Date: 2019/7/29
     */
    @PostMapping("/getMyEarnings")
    @CheckParams(checkAuth = true,params = {"businessId"})
    public Map<String,Object> getMyEarnings(@RequestBody ParamsMap paramsMap){
        return bmsUserBusinessService.getMyEarnings(paramsMap);
    }

    /**
     * @Description 获取我的核销记录
     * @Author: cfx
     * @Date: 2019/7/29
     */
    @PostMapping("/getMyVerRecord")
    @CheckParams(checkAuth = true,params = {"businessId"})
    public Map<String,Object> getMyVerRecord(@RequestBody ParamsMap paramsMap){
        return bmsUserBusinessService.getMyVerRecord(paramsMap);
    }


    /**
     * @Description 获取我的核销记录
     * @Author: cfx
     * @Date: 2019/7/29
     */
    @PostMapping("/getVerRecordDetails")
    @CheckParams(checkAuth = true,params = {"businessId","verRecordId"})
    public Map<String,Object> getVerRecordDetails(@RequestBody ParamsMap paramsMap){
        return bmsUserBusinessService.getVerRecordDetails(paramsMap);
    }

}
